home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-06-07 | 59.8 KB | 2,383 lines |
- Path: uunet!tektronix!tekgen!tekred!games
- From: games@tekred.TEK.COM
- Newsgroups: comp.sources.games
- Subject: v04i031: chtrans - a chess notation translator (standard to Cartesian algebraic)
- Message-ID: <2613@tekred.TEK.COM>
- Date: 7 Jun 88 23:09:52 GMT
- Sender: billr@tekred.TEK.COM
- Lines: 2372
- Approved: billr@saab.CNA.TEK.COM
-
- Submitted by: Ray Balogh <rabalogh@ccng.waterloo.edu>
- Comp.sources.games: Volume 4, Issue 31
- Archive-name: chtrans
-
- [See also, previous posting for displaying moves on a vt240
- using ReGIS graphics. -br]
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of shell archive."
- # Contents: README Makefile chtrans.c regerror.c regexp.c regexp.h
- # regmagic.h trmoves.c
- # Wrapped by billr@saab on Tue Jun 7 16:08:00 1988
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f README -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file \"README\"
- else
- echo shar: Extracting \"README\" \(1679 characters\)
- sed "s/^X//" >README <<'END_OF_README'
- X
- XChtrans
- X-------
- X
- X SYNOPSIS: chtrans
- X
- X DESCRIPTION:
- X
- X A filter for translating standard algebraic chess notation (e.g., d:c)
- X to Cartesian algebraic (e.g., d5c4). It read from the standard input
- X and writes to the standard output only. It is based on a regualar
- X expression package (similar to regex(3)) written by Henry Spencer
- X at the University of Toronto (only the portion of the package needed
- X for chtrans is included), and on legal move generation code
- X derived from John Stanback's chess program.
- X
- X The input is treated as "words" (blank delimited tokens), and any word
- X that is not entirely a syntactically correct move is discarded.
- X Commented text i.e. (...) or #...<newline> is ignored (the opening
- X character must be separated from the end of the preceding move by
- X white space). Parenthesized comments nest, and a '(' need not be
- X matched by a ')' -- the remainder of the file will be ignored.
- X
- X Verbose diagnostics are printed on illegal moves, if the program
- X is compiled with the -DVERBOSE option.
- X
- X Before piping the output of this program to anything, run it alone
- X to make sure the input is translated without error.
- X
- X SUGGESTIONS:
- X
- X The verbose diagnostic feature should be a command line option.
- X
- X Other types of translations should be supported.
- X
- X BUGS:
- X
- X Was written without any kind of published standard or precisely
- X defined grammar, so some forms such as "de:" are not understood.
- X
- X Commented text should not have to be separated from the preceding
- X word by white space. The same is true for the move numbers,
- X i.e., "1.e2e4" should be accepted.
- END_OF_README
- if test 1679 -ne `wc -c <README`; then
- echo shar: \"README\" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- if test -f Makefile -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file \"Makefile\"
- else
- echo shar: Extracting \"Makefile\" \(345 characters\)
- sed "s/^X//" >Makefile <<'END_OF_Makefile'
- X# Makefile for chtrans (notation translator)
- X
- XT_FLAGS = -DBSD -DVERBOSE
- XT_OBJ = chtrans.o trmoves.o
- XT_INC = regexp.h
- XTLIB_OBJ = regexp.o regerror.o
- XTLIB_INC = regexp.h regmagic.h
- X
- X####
- X
- Xall: chtrans $(TLIB_OBJ)
- X
- Xchtrans: $(T_OBJ) $(TLIB_OBJ)
- X cc -o chtrans $(T_FLAGS) $(T_OBJ) $(TLIB_OBJ)
- X
- X$(T_OBJ): $(T_INC)
- X$(TLIB_OBJ): $(TLIB_INC)
- X
- END_OF_Makefile
- if test 345 -ne `wc -c <Makefile`; then
- echo shar: \"Makefile\" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- if test -f chtrans.c -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file \"chtrans.c\"
- else
- echo shar: Extracting \"chtrans.c\" \(4003 characters\)
- sed "s/^X//" >chtrans.c <<'END_OF_chtrans.c'
- X/*
- X * Copyright (c) 1987 by Ray Balogh (rabalogh@ccng.waterloo.edu)
- X *
- X * Permission is granted to anyone to use this software for any
- X * purpose on any computer system, and to redistribute it freely,
- X * subject to the following restrictions:
- X *
- X * 1. The author is not responsible for the consequences of use of
- X * this software, no matter how awful, even if they arise
- X * from defects in it.
- X *
- X * 2. The origin of this software must not be misrepresented, either
- X * by explicit claim or by omission.
- X *
- X * 3. Altered versions must be plainly marked as such, and must not
- X * be misrepresented as being the original software.
- X *
- X */
- X
- X/* see "regexp.c" for the copyright notice pertaining to the regular
- X * expression parser.
- X */
- X
- X#include <stdio.h>
- X#include "regexp.h"
- X
- X#define DUNNO '_'
- X
- Xchar buf[20];
- X
- Xchar gram1_castle[] =
- X "^[oO0]-[oO0](-[oO0])?(ch|mt|[+])?[!?,.;]*$";
- Xchar gram1_general[] =
- X "^([NBRQK]?)([a-h]?)([1-8]?)([xX:-]?)([a-h]?)([1-8]?)(ch|mt|[+])?[!?,.;]*$";
- Xchar gram1_ignore[] =
- X"^([0-9]+\\.?|a|be|each|he|ch|mt|Re:)$";
- X
- X#define SUBEX(p,i) (p->startp[i])
- X#define NILSUBEX(p,i) (p->endp[i] == p->startp[i])
- X#define SUBEXLEN(p,i) (p->endp[i] - p->startp[i])
- X#define BDCHAR(p,i) ((!NILSUBEX(p,i)) ? *SUBEX(p,i) : DUNNO)
- X
- X
- X/* gobble (possibly nested) comments */
- Xgobble( s, openc, closec )
- Xchar *s, openc, closec;
- X{
- X int c;
- X
- X while ( *++s != '\0' )
- X if ( *s == openc )
- X gobble( s, openc, closec );
- X else if ( *s == closec )
- X return;
- X while ( (c = getchar()) != EOF && c != (int)closec )
- X if ( c == (int)openc )
- X gobble( " ", openc, closec );
- X}
- X
- X
- X/* check if s contains any valid move spec. char */
- Xint
- Xtestmove( s )
- Xchar *s;
- X{
- X static int testtab[128] = {
- X 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- X 0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,-1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
- X 0,0,50,0,0,0,0,0,0,0,0,50,0,0,50,-1,0,50,50,0,0,0,0,0,0,0,0,0,0,0,0,0,
- X 0,50,50,50,50,50,50,50,50,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
- X };
- X register int sum;
- X register char *p;
- X
- X for ( sum = 0, p = s; *p != '\0' ; p++ )
- X sum += testtab[ (int)*p & 0xFF ];
- X return ( sum > 50 || sum < -2 );
- X}
- X
- X
- X/* ARGSUSED */
- Xmain(argc, argv)
- Xint argc;
- Xchar *argv[];
- X{
- X regexp *g1_ignore, *g1_castle, *g1_general, *expr;
- X int i,j;
- X char piece, sep, f1, r1, f2, r2;
- X char s[256];
- X static int ply = 0;
- X
- X if ( (g1_ignore = regcomp(gram1_ignore)) == NULL ||
- X (g1_castle = regcomp(gram1_castle)) == NULL ||
- X (g1_general = regcomp(gram1_general)) == NULL ) {
- X fprintf( stderr, "bad grammar\n" );
- X exit(1);
- X }
- X
- X while ( scanf("%s",s) == 1 ) {
- X
- X /* gobble comments */
- X if ( *s == '#' ) {
- X gobble( s, '\0', '\n' );
- X continue;
- X }
- X if ( *s == '(' ) {
- X gobble( s, '(', ')' );
- X continue;
- X }
- X /* DEBUG fprintf( stderr, "[%s] ", s ); */
- X
- X if ( ! testmove(s) ) /* make cursory validity check */
- X continue;
- X
- X if ( regexec(g1_ignore, s) == 1 ) {
- X continue;
- X } else
- X if ( regexec(g1_castle, s) == 1 ) {
- X expr = g1_castle;
- X if ( expr->startp[1] == expr->endp[1] ) /* king's side */
- X {sprintf( buf, "Ke%c-g%c", DUNNO, DUNNO ); trans(buf, ++ply);}
- X else
- X {sprintf( buf, "Ke%c-c%c", DUNNO, DUNNO ); trans(buf, ++ply);}
- X continue;
- X } else
- X if ( regexec(g1_general, s) == 1 ) {
- X expr = g1_general;
- X piece = BDCHAR(expr,1);
- X f1 = BDCHAR(expr,2); r1 = BDCHAR(expr,3);
- X f2 = BDCHAR(expr,5); r2 = BDCHAR(expr,6);
- X sep = BDCHAR(expr,4);
- X
- X if ( f2 == DUNNO && r2 == DUNNO ) {
- X f2 = f1; r2 = r1;
- X f1 = DUNNO; r1 = DUNNO;
- X }
- X if ( piece == DUNNO )
- X piece = 'P';
- X /* if sep is not given, leave it as unspecified
- X if ( sep == DUNNO )
- X sep = '-';
- X */
- X if (sep != '-' && sep != DUNNO)
- X sep = ':';
- X sprintf( buf, "%c%c%c%c%c%c%.*s",
- X piece, f1, r1, sep, f2, r2, SUBEXLEN(expr,7), SUBEX(expr,7) );
- X trans(buf, ++ply);
- X continue;
- X } else {
- X /* fprintf( stderr, "syntax error\n" ); */
- X continue;
- X }
- X }
- X exit(0);
- X}
- END_OF_chtrans.c
- if test 4003 -ne `wc -c <chtrans.c`; then
- echo shar: \"chtrans.c\" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- if test -f regerror.c -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file \"regerror.c\"
- else
- echo shar: Extracting \"regerror.c\" \(224 characters\)
- sed "s/^X//" >regerror.c <<'END_OF_regerror.c'
- X#include <stdio.h>
- X
- Xvoid
- Xregerror(s)
- Xchar *s;
- X{
- X#ifdef ERRAVAIL
- X error("regexp: %s", s);
- X#else
- X/*
- X fprintf(stderr, "regexp(3): %s\n", s);
- X exit(1);
- X*/
- X return; /* let std. egrep handle errors */
- X#endif
- X /* NOTREACHED */
- X}
- END_OF_regerror.c
- if test 224 -ne `wc -c <regerror.c`; then
- echo shar: \"regerror.c\" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- if test -f regexp.c -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file \"regexp.c\"
- else
- echo shar: Extracting \"regexp.c\" \(27784 characters\)
- sed "s/^X//" >regexp.c <<'END_OF_regexp.c'
- X/*
- X * regcomp and regexec -- regsub and regerror are elsewhere
- X *
- X * Copyright (c) 1986 by University of Toronto.
- X * Written by Henry Spencer. Not derived from licensed software.
- X *
- X * Permission is granted to anyone to use this software for any
- X * purpose on any computer system, and to redistribute it freely,
- X * subject to the following restrictions:
- X *
- X * 1. The author is not responsible for the consequences of use of
- X * this software, no matter how awful, even if they arise
- X * from defects in it.
- X *
- X * 2. The origin of this software must not be misrepresented, either
- X * by explicit claim or by omission.
- X *
- X * 3. Altered versions must be plainly marked as such, and must not
- X * be misrepresented as being the original software.
- X *
- X * Beware that some of this code is subtly aware of the way operator
- X * precedence is structured in regular expressions. Serious changes in
- X * regular-expression syntax might require a total rethink.
- X */
- X#include <stdio.h>
- X#include "regexp.h"
- X#include "regmagic.h"
- X
- X/*
- X * The "internal use only" fields in regexp.h are present to pass info from
- X * compile to execute that permits the execute phase to run lots faster on
- X * simple cases. They are:
- X *
- X * regstart char that must begin a match; '\0' if none obvious
- X * reganch is the match anchored (at beginning-of-line only)?
- X * regmust string (pointer into program) that match must include, or NULL
- X * regmlen length of regmust string
- X *
- X * Regstart and reganch permit very fast decisions on suitable starting points
- X * for a match, cutting down the work a lot. Regmust permits fast rejection
- X * of lines that cannot possibly match. The regmust tests are costly enough
- X * that regcomp() supplies a regmust only if the r.e. contains something
- X * potentially expensive (at present, the only such thing detected is * or +
- X * at the start of the r.e., which can involve a lot of backup). Regmlen is
- X * supplied because the test in regexec() needs it and regcomp() is computing
- X * it anyway.
- X */
- X
- X/*
- X * Structure for regexp "program". This is essentially a linear encoding
- X * of a nondeterministic finite-state machine (aka syntax charts or
- X * "railroad normal form" in parsing technology). Each node is an opcode
- X * plus a "next" pointer, possibly plus an operand. "Next" pointers of
- X * all nodes except BRANCH implement concatenation; a "next" pointer with
- X * a BRANCH on both ends of it is connecting two alternatives. (Here we
- X * have one of the subtle syntax dependencies: an individual BRANCH (as
- X * opposed to a collection of them) is never concatenated with anything
- X * because of operator precedence.) The operand of some types of node is
- X * a literal string; for others, it is a node leading into a sub-FSM. In
- X * particular, the operand of a BRANCH node is the first node of the branch.
- X * (NB this is *not* a tree structure: the tail of the branch connects
- X * to the thing following the set of BRANCHes.) The opcodes are:
- X */
- X
- X/* definition number opnd? meaning */
- X#define END 0 /* no End of program. */
- X#define BOL 1 /* no Match "" at beginning of line. */
- X#define EOL 2 /* no Match "" at end of line. */
- X#define ANY 3 /* no Match any one character. */
- X#define ANYOF 4 /* str Match any character in this string. */
- X#define ANYBUT 5 /* str Match any character not in this string. */
- X#define BRANCH 6 /* node Match this alternative, or the next... */
- X#define BACK 7 /* no Match "", "next" ptr points backward. */
- X#define EXACTLY 8 /* str Match this string. */
- X#define NOTHING 9 /* no Match empty string. */
- X#define STAR 10 /* node Match this (simple) thing 0 or more times. */
- X#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
- X#define OPEN 20 /* no Mark this point in input as start of #n. */
- X /* OPEN+1 is number 1, etc. */
- X#define CLOSE 30 /* no Analogous to OPEN. */
- X
- X/*
- X * Opcode notes:
- X *
- X * BRANCH The set of branches constituting a single choice are hooked
- X * together with their "next" pointers, since precedence prevents
- X * anything being concatenated to any individual branch. The
- X * "next" pointer of the last BRANCH in a choice points to the
- X * thing following the whole choice. This is also where the
- X * final "next" pointer of each individual branch points; each
- X * branch starts with the operand node of a BRANCH node.
- X *
- X * BACK Normal "next" pointers all implicitly point forward; BACK
- X * exists to make loop structures possible.
- X *
- X * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
- X * BRANCH structures using BACK. Simple cases (one character
- X * per match) are implemented with STAR and PLUS for speed
- X * and to minimize recursive plunges.
- X *
- X * OPEN,CLOSE ...are numbered at compile time.
- X */
- X
- X/*
- X * A node is one char of opcode followed by two chars of "next" pointer.
- X * "Next" pointers are stored as two 8-bit pieces, high order first. The
- X * value is a positive offset from the opcode of the node containing it.
- X * An operand, if any, simply follows the node. (Note that much of the
- X * code generation knows about this implicit relationship.)
- X *
- X * Using two bytes for the "next" pointer is vast overkill for most things,
- X * but allows patterns to get big without disasters.
- X */
- X#define OP(p) (*(p))
- X#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
- X#define OPERAND(p) ((p) + 3)
- X
- X/*
- X * See regmagic.h for one further detail of program structure.
- X */
- X
- X
- X/*
- X * Utility definitions.
- X */
- X#ifndef CHARBITS
- X#define UCHARAT(p) ((int)*(unsigned char *)(p))
- X#else
- X#define UCHARAT(p) ((int)*(p)&CHARBITS)
- X#endif
- X
- X#define FAIL(m) { regerror(m); return(NULL); }
- X#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
- X#define META "^$.[()|?+*\\"
- X
- X/*
- X * Flags to be passed up and down.
- X */
- X#define HASWIDTH 01 /* Known never to match null string. */
- X#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
- X#define SPSTART 04 /* Starts with * or +. */
- X#define WORST 0 /* Worst case. */
- X
- X/*
- X * Global work variables for regcomp().
- X */
- Xstatic char *regparse; /* Input-scan pointer. */
- Xstatic int regnpar; /* () count. */
- Xstatic char regdummy;
- Xstatic char *regcode; /* Code-emit pointer; ®dummy = don't. */
- Xstatic long regsize; /* Code size. */
- X
- X/*
- X * Forward declarations for regcomp()'s friends.
- X */
- X#ifndef STATIC
- X#define STATIC static
- X#endif
- XSTATIC char *reg();
- XSTATIC char *regbranch();
- XSTATIC char *regpiece();
- XSTATIC char *regatom();
- XSTATIC char *regnode();
- XSTATIC char *regnext();
- XSTATIC void regc();
- XSTATIC void reginsert();
- XSTATIC void regtail();
- XSTATIC void regoptail();
- X#ifdef STRCSPN
- XSTATIC int strcspn();
- X#endif
- X
- X/*
- X - regcomp - compile a regular expression into internal code
- X *
- X * We can't allocate space until we know how big the compiled form will be,
- X * but we can't compile it (and thus know how big it is) until we've got a
- X * place to put the code. So we cheat: we compile it twice, once with code
- X * generation turned off and size counting turned on, and once "for real".
- X * This also means that we don't allocate space until we are sure that the
- X * thing really will compile successfully, and we never have to move the
- X * code and thus invalidate pointers into it. (Note that it has to be in
- X * one piece because free() must be able to free it all.)
- X *
- X * Beware that the optimization-preparation code in here knows about some
- X * of the structure of the compiled regexp.
- X */
- Xregexp *
- Xregcomp(exp)
- Xchar *exp;
- X{
- X register regexp *r;
- X register char *scan;
- X register char *longest;
- X register int len;
- X int flags;
- X extern char *malloc();
- X
- X if (exp == NULL)
- X FAIL("NULL argument");
- X
- X /* First pass: determine size, legality. */
- X regparse = exp;
- X regnpar = 1;
- X regsize = 0L;
- X regcode = ®dummy;
- X regc(MAGIC);
- X if (reg(0, &flags) == NULL)
- X return(NULL);
- X
- X /* Small enough for pointer-storage convention? */
- X if (regsize >= 32767L) /* Probably could be 65535L. */
- X FAIL("regexp too big");
- X
- X /* Allocate space. */
- X r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
- X if (r == NULL)
- X FAIL("out of space");
- X
- X /* Second pass: emit code. */
- X regparse = exp;
- X regnpar = 1;
- X regcode = r->program;
- X regc(MAGIC);
- X if (reg(0, &flags) == NULL)
- X return(NULL);
- X
- X /* Dig out information for optimizations. */
- X r->regstart = '\0'; /* Worst-case defaults. */
- X r->reganch = 0;
- X r->regmust = NULL;
- X r->regmlen = 0;
- X scan = r->program+1; /* First BRANCH. */
- X if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
- X scan = OPERAND(scan);
- X
- X /* Starting-point info. */
- X if (OP(scan) == EXACTLY)
- X r->regstart = *OPERAND(scan);
- X else if (OP(scan) == BOL)
- X r->reganch++;
- X
- X /*
- X * If there's something expensive in the r.e., find the
- X * longest literal string that must appear and make it the
- X * regmust. Resolve ties in favor of later strings, since
- X * the regstart check works with the beginning of the r.e.
- X * and avoiding duplication strengthens checking. Not a
- X * strong reason, but sufficient in the absence of others.
- X */
- X if (flags&SPSTART) {
- X longest = NULL;
- X len = 0;
- X for (; scan != NULL; scan = regnext(scan))
- X if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
- X longest = OPERAND(scan);
- X len = strlen(OPERAND(scan));
- X }
- X r->regmust = longest;
- X r->regmlen = len;
- X }
- X }
- X
- X return(r);
- X}
- X
- X/*
- X - reg - regular expression, i.e. main body or parenthesized thing
- X *
- X * Caller must absorb opening parenthesis.
- X *
- X * Combining parenthesis handling with the base level of regular expression
- X * is a trifle forced, but the need to tie the tails of the branches to what
- X * follows makes it hard to avoid.
- X */
- Xstatic char *
- Xreg(paren, flagp)
- Xint paren; /* Parenthesized? */
- Xint *flagp;
- X{
- X register char *ret;
- X register char *br;
- X register char *ender;
- X register int parno;
- X int flags;
- X
- X *flagp = HASWIDTH; /* Tentatively. */
- X
- X /* Make an OPEN node, if parenthesized. */
- X if (paren) {
- X if (regnpar >= NSUBEXP)
- X FAIL("too many ()");
- X parno = regnpar;
- X regnpar++;
- X ret = regnode(OPEN+parno);
- X } else
- X ret = NULL;
- X
- X /* Pick up the branches, linking them together. */
- X br = regbranch(&flags);
- X if (br == NULL)
- X return(NULL);
- X if (ret != NULL)
- X regtail(ret, br); /* OPEN -> first. */
- X else
- X ret = br;
- X if (!(flags&HASWIDTH))
- X *flagp &= ~HASWIDTH;
- X *flagp |= flags&SPSTART;
- X while (*regparse == '|') {
- X regparse++;
- X br = regbranch(&flags);
- X if (br == NULL)
- X return(NULL);
- X regtail(ret, br); /* BRANCH -> BRANCH. */
- X if (!(flags&HASWIDTH))
- X *flagp &= ~HASWIDTH;
- X *flagp |= flags&SPSTART;
- X }
- X
- X /* Make a closing node, and hook it on the end. */
- X ender = regnode((paren) ? CLOSE+parno : END);
- X regtail(ret, ender);
- X
- X /* Hook the tails of the branches to the closing node. */
- X for (br = ret; br != NULL; br = regnext(br))
- X regoptail(br, ender);
- X
- X /* Check for proper termination. */
- X if (paren && *regparse++ != ')') {
- X FAIL("unmatched ()");
- X } else if (!paren && *regparse != '\0') {
- X if (*regparse == ')') {
- X FAIL("unmatched ()");
- X } else
- X FAIL("junk on end"); /* "Can't happen". */
- X /* NOTREACHED */
- X }
- X
- X return(ret);
- X}
- X
- X/*
- X - regbranch - one alternative of an | operator
- X *
- X * Implements the concatenation operator.
- X */
- Xstatic char *
- Xregbranch(flagp)
- Xint *flagp;
- X{
- X register char *ret;
- X register char *chain;
- X register char *latest;
- X int flags;
- X
- X *flagp = WORST; /* Tentatively. */
- X
- X ret = regnode(BRANCH);
- X chain = NULL;
- X while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
- X latest = regpiece(&flags);
- X if (latest == NULL)
- X return(NULL);
- X *flagp |= flags&HASWIDTH;
- X if (chain == NULL) /* First piece. */
- X *flagp |= flags&SPSTART;
- X else
- X regtail(chain, latest);
- X chain = latest;
- X }
- X if (chain == NULL) /* Loop ran zero times. */
- X (void) regnode(NOTHING);
- X
- X return(ret);
- X}
- X
- X/*
- X - regpiece - something followed by possible [*+?]
- X *
- X * Note that the branching code sequences used for ? and the general cases
- X * of * and + are somewhat optimized: they use the same NOTHING node as
- X * both the endmarker for their branch list and the body of the last branch.
- X * It might seem that this node could be dispensed with entirely, but the
- X * endmarker role is not redundant.
- X */
- Xstatic char *
- Xregpiece(flagp)
- Xint *flagp;
- X{
- X register char *ret;
- X register char op;
- X register char *next;
- X int flags;
- X
- X ret = regatom(&flags);
- X if (ret == NULL)
- X return(NULL);
- X
- X op = *regparse;
- X if (!ISMULT(op)) {
- X *flagp = flags;
- X return(ret);
- X }
- X
- X if (!(flags&HASWIDTH) && op != '?')
- X FAIL("*+ operand could be empty");
- X *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
- X
- X if (op == '*' && (flags&SIMPLE))
- X reginsert(STAR, ret);
- X else if (op == '*') {
- X /* Emit x* as (x&|), where & means "self". */
- X reginsert(BRANCH, ret); /* Either x */
- X regoptail(ret, regnode(BACK)); /* and loop */
- X regoptail(ret, ret); /* back */
- X regtail(ret, regnode(BRANCH)); /* or */
- X regtail(ret, regnode(NOTHING)); /* null. */
- X } else if (op == '+' && (flags&SIMPLE))
- X reginsert(PLUS, ret);
- X else if (op == '+') {
- X /* Emit x+ as x(&|), where & means "self". */
- X next = regnode(BRANCH); /* Either */
- X regtail(ret, next);
- X regtail(regnode(BACK), ret); /* loop back */
- X regtail(next, regnode(BRANCH)); /* or */
- X regtail(ret, regnode(NOTHING)); /* null. */
- X } else if (op == '?') {
- X /* Emit x? as (x|) */
- X reginsert(BRANCH, ret); /* Either x */
- X regtail(ret, regnode(BRANCH)); /* or */
- X next = regnode(NOTHING); /* null. */
- X regtail(ret, next);
- X regoptail(ret, next);
- X }
- X regparse++;
- X if (ISMULT(*regparse))
- X FAIL("nested *?+");
- X
- X return(ret);
- X}
- X
- X/*
- X - regatom - the lowest level
- X *
- X * Optimization: gobbles an entire sequence of ordinary characters so that
- X * it can turn them into a single node, which is smaller to store and
- X * faster to run. Backslashed characters are exceptions, each becoming a
- X * separate node; the code is simpler that way and it's not worth fixing.
- X */
- Xstatic char *
- Xregatom(flagp)
- Xint *flagp;
- X{
- X register char *ret;
- X int flags;
- X
- X *flagp = WORST; /* Tentatively. */
- X
- X switch (*regparse++) {
- X case '^':
- X ret = regnode(BOL);
- X break;
- X case '$':
- X ret = regnode(EOL);
- X break;
- X case '.':
- X ret = regnode(ANY);
- X *flagp |= HASWIDTH|SIMPLE;
- X break;
- X case '[': {
- X register int class;
- X register int classend;
- X
- X if (*regparse == '^') { /* Complement of range. */
- X ret = regnode(ANYBUT);
- X regparse++;
- X } else
- X ret = regnode(ANYOF);
- X if (*regparse == ']' || *regparse == '-')
- X regc(*regparse++);
- X while (*regparse != '\0' && *regparse != ']') {
- X if (*regparse == '-') {
- X regparse++;
- X if (*regparse == ']' || *regparse == '\0')
- X regc('-');
- X else {
- X class = UCHARAT(regparse-2)+1;
- X classend = UCHARAT(regparse);
- X if (class > classend+1)
- X FAIL("invalid [] range");
- X for (; class <= classend; class++)
- X regc(class);
- X regparse++;
- X }
- X } else
- X regc(*regparse++);
- X }
- X regc('\0');
- X if (*regparse != ']')
- X FAIL("unmatched []");
- X regparse++;
- X *flagp |= HASWIDTH|SIMPLE;
- X }
- X break;
- X case '(':
- X ret = reg(1, &flags);
- X if (ret == NULL)
- X return(NULL);
- X *flagp |= flags&(HASWIDTH|SPSTART);
- X break;
- X case '\0':
- X case '|':
- X case ')':
- X FAIL("internal urp"); /* Supposed to be caught earlier. */
- X /* NOTREACHED */
- X /*break;*/
- X case '?':
- X case '+':
- X case '*':
- X FAIL("?+* follows nothing");
- X /* NOTREACHED */
- X /*break;*/
- X case '\\':
- X if (*regparse == '\0')
- X FAIL("trailing \\");
- X ret = regnode(EXACTLY);
- X regc(*regparse++);
- X regc('\0');
- X *flagp |= HASWIDTH|SIMPLE;
- X break;
- X default: {
- X register int len;
- X register char ender;
- X
- X regparse--;
- X len = strcspn(regparse, META);
- X if (len <= 0)
- X FAIL("internal disaster");
- X ender = *(regparse+len);
- X if (len > 1 && ISMULT(ender))
- X len--; /* Back off clear of ?+* operand. */
- X *flagp |= HASWIDTH;
- X if (len == 1)
- X *flagp |= SIMPLE;
- X ret = regnode(EXACTLY);
- X while (len > 0) {
- X regc(*regparse++);
- X len--;
- X }
- X regc('\0');
- X }
- X break;
- X }
- X
- X return(ret);
- X}
- X
- X/*
- X - regnode - emit a node
- X */
- Xstatic char * /* Location. */
- Xregnode(op)
- Xchar op;
- X{
- X register char *ret;
- X register char *ptr;
- X
- X ret = regcode;
- X if (ret == ®dummy) {
- X regsize += 3;
- X return(ret);
- X }
- X
- X ptr = ret;
- X *ptr++ = op;
- X *ptr++ = '\0'; /* Null "next" pointer. */
- X *ptr++ = '\0';
- X regcode = ptr;
- X
- X return(ret);
- X}
- X
- X/*
- X - regc - emit (if appropriate) a byte of code
- X */
- Xstatic void
- Xregc(b)
- Xchar b;
- X{
- X if (regcode != ®dummy)
- X *regcode++ = b;
- X else
- X regsize++;
- X}
- X
- X/*
- X - reginsert - insert an operator in front of already-emitted operand
- X *
- X * Means relocating the operand.
- X */
- Xstatic void
- Xreginsert(op, opnd)
- Xchar op;
- Xchar *opnd;
- X{
- X register char *src;
- X register char *dst;
- X register char *place;
- X
- X if (regcode == ®dummy) {
- X regsize += 3;
- X return;
- X }
- X
- X src = regcode;
- X regcode += 3;
- X dst = regcode;
- X while (src > opnd)
- X *--dst = *--src;
- X
- X place = opnd; /* Op node, where operand used to be. */
- X *place++ = op;
- X *place++ = '\0';
- X *place++ = '\0';
- X}
- X
- X/*
- X - regtail - set the next-pointer at the end of a node chain
- X */
- Xstatic void
- Xregtail(p, val)
- Xchar *p;
- Xchar *val;
- X{
- X register char *scan;
- X register char *temp;
- X register int offset;
- X
- X if (p == ®dummy)
- X return;
- X
- X /* Find last node. */
- X scan = p;
- X for (;;) {
- X temp = regnext(scan);
- X if (temp == NULL)
- X break;
- X scan = temp;
- X }
- X
- X if (OP(scan) == BACK)
- X offset = scan - val;
- X else
- X offset = val - scan;
- X *(scan+1) = (offset>>8)&0377;
- X *(scan+2) = offset&0377;
- X}
- X
- X/*
- X - regoptail - regtail on operand of first argument; nop if operandless
- X */
- Xstatic void
- Xregoptail(p, val)
- Xchar *p;
- Xchar *val;
- X{
- X /* "Operandless" and "op != BRANCH" are synonymous in practice. */
- X if (p == NULL || p == ®dummy || OP(p) != BRANCH)
- X return;
- X regtail(OPERAND(p), val);
- X}
- X
- X/*
- X * regexec and friends
- X */
- X
- X/*
- X * Global work variables for regexec().
- X */
- Xstatic char *reginput; /* String-input pointer. */
- Xstatic char *regbol; /* Beginning of input, for ^ check. */
- Xstatic char **regstartp; /* Pointer to startp array. */
- Xstatic char **regendp; /* Ditto for endp. */
- X
- X/*
- X * Forwards.
- X */
- XSTATIC int regtry();
- XSTATIC int regmatch();
- XSTATIC int regrepeat();
- X
- X#ifdef DEBUG
- Xint regnarrate = 0;
- Xvoid regdump();
- XSTATIC char *regprop();
- X#endif
- X
- X/*
- X - regexec - match a regexp against a string
- X */
- Xint
- Xregexec(prog, string)
- Xregister regexp *prog;
- Xregister char *string;
- X{
- X register char *s;
- X extern char *strchr();
- X
- X /* Be paranoid... */
- X if (prog == NULL || string == NULL) {
- X regerror("NULL parameter");
- X return(0);
- X }
- X
- X /* Check validity of program. */
- X if (UCHARAT(prog->program) != MAGIC) {
- X regerror("corrupted program");
- X return(0);
- X }
- X
- X /* If there is a "must appear" string, look for it. */
- X if (prog->regmust != NULL) {
- X s = string;
- X while ((s = strchr(s, prog->regmust[0])) != NULL) {
- X if (strncmp(s, prog->regmust, prog->regmlen) == 0)
- X break; /* Found it. */
- X s++;
- X }
- X if (s == NULL) /* Not present. */
- X return(0);
- X }
- X
- X /* Mark beginning of line for ^ . */
- X regbol = string;
- X
- X /* Simplest case: anchored match need be tried only once. */
- X if (prog->reganch)
- X return(regtry(prog, string));
- X
- X /* Messy cases: unanchored match. */
- X s = string;
- X if (prog->regstart != '\0')
- X /* We know what char it must start with. */
- X while ((s = strchr(s, prog->regstart)) != NULL) {
- X if (regtry(prog, s))
- X return(1);
- X s++;
- X }
- X else
- X /* We don't -- general case. */
- X do {
- X if (regtry(prog, s))
- X return(1);
- X } while (*s++ != '\0');
- X
- X /* Failure. */
- X return(0);
- X}
- X
- X/*
- X - regtry - try match at specific point
- X */
- Xstatic int /* 0 failure, 1 success */
- Xregtry(prog, string)
- Xregexp *prog;
- Xchar *string;
- X{
- X register int i;
- X register char **sp;
- X register char **ep;
- X
- X reginput = string;
- X regstartp = prog->startp;
- X regendp = prog->endp;
- X
- X sp = prog->startp;
- X ep = prog->endp;
- X for (i = NSUBEXP; i > 0; i--) {
- X *sp++ = NULL;
- X *ep++ = NULL;
- X }
- X if (regmatch(prog->program + 1)) {
- X prog->startp[0] = string;
- X prog->endp[0] = reginput;
- X return(1);
- X } else
- X return(0);
- X}
- X
- X/*
- X - regmatch - main matching routine
- X *
- X * Conceptually the strategy is simple: check to see whether the current
- X * node matches, call self recursively to see whether the rest matches,
- X * and then act accordingly. In practice we make some effort to avoid
- X * recursion, in particular by going through "ordinary" nodes (that don't
- X * need to know whether the rest of the match failed) by a loop instead of
- X * by recursion.
- X */
- Xstatic int /* 0 failure, 1 success */
- Xregmatch(prog)
- Xchar *prog;
- X{
- X register char *scan; /* Current node. */
- X char *next; /* Next node. */
- X extern char *strchr();
- X
- X scan = prog;
- X#ifdef DEBUG
- X if (scan != NULL && regnarrate)
- X fprintf(stderr, "%s(\n", regprop(scan));
- X#endif
- X while (scan != NULL) {
- X#ifdef DEBUG
- X if (regnarrate)
- X fprintf(stderr, "%s...\n", regprop(scan));
- X#endif
- X next = regnext(scan);
- X
- X switch (OP(scan)) {
- X case BOL:
- X if (reginput != regbol)
- X return(0);
- X break;
- X case EOL:
- X if (*reginput != '\0')
- X return(0);
- X break;
- X case ANY:
- X if (*reginput == '\0')
- X return(0);
- X reginput++;
- X break;
- X case EXACTLY: {
- X register int len;
- X register char *opnd;
- X
- X opnd = OPERAND(scan);
- X /* Inline the first character, for speed. */
- X if (*opnd != *reginput)
- X return(0);
- X len = strlen(opnd);
- X if (len > 1 && strncmp(opnd, reginput, len) != 0)
- X return(0);
- X reginput += len;
- X }
- X break;
- X case ANYOF:
- X if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
- X return(0);
- X reginput++;
- X break;
- X case ANYBUT:
- X if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
- X return(0);
- X reginput++;
- X break;
- X case NOTHING:
- X break;
- X case BACK:
- X break;
- X case OPEN+1:
- X case OPEN+2:
- X case OPEN+3:
- X case OPEN+4:
- X case OPEN+5:
- X case OPEN+6:
- X case OPEN+7:
- X case OPEN+8:
- X case OPEN+9: {
- X register int no;
- X register char *save;
- X
- X no = OP(scan) - OPEN;
- X save = reginput;
- X
- X if (regmatch(next)) {
- X /*
- X * Don't set startp if some later
- X * invocation of the same parentheses
- X * already has.
- X */
- X if (regstartp[no] == NULL)
- X regstartp[no] = save;
- X return(1);
- X } else
- X return(0);
- X }
- X /* NOTREACHED */
- X /*break;*/
- X case CLOSE+1:
- X case CLOSE+2:
- X case CLOSE+3:
- X case CLOSE+4:
- X case CLOSE+5:
- X case CLOSE+6:
- X case CLOSE+7:
- X case CLOSE+8:
- X case CLOSE+9: {
- X register int no;
- X register char *save;
- X
- X no = OP(scan) - CLOSE;
- X save = reginput;
- X
- X if (regmatch(next)) {
- X /*
- X * Don't set endp if some later
- X * invocation of the same parentheses
- X * already has.
- X */
- X if (regendp[no] == NULL)
- X regendp[no] = save;
- X return(1);
- X } else
- X return(0);
- X }
- X /* NOTREACHED */
- X /*break;*/
- X case BRANCH: {
- X register char *save;
- X
- X if (OP(next) != BRANCH) /* No choice. */
- X next = OPERAND(scan); /* Avoid recursion. */
- X else {
- X do {
- X save = reginput;
- X if (regmatch(OPERAND(scan)))
- X return(1);
- X reginput = save;
- X scan = regnext(scan);
- X } while (scan != NULL && OP(scan) == BRANCH);
- X return(0);
- X /* NOTREACHED */
- X }
- X }
- X break;
- X case STAR:
- X case PLUS: {
- X register char nextch;
- X register int no;
- X register char *save;
- X register int min;
- X
- X /*
- X * Lookahead to avoid useless match attempts
- X * when we know what character comes next.
- X */
- X nextch = '\0';
- X if (OP(next) == EXACTLY)
- X nextch = *OPERAND(next);
- X min = (OP(scan) == STAR) ? 0 : 1;
- X save = reginput;
- X no = regrepeat(OPERAND(scan));
- X while (no >= min) {
- X /* If it could work, try it. */
- X if (nextch == '\0' || *reginput == nextch)
- X if (regmatch(next))
- X return(1);
- X /* Couldn't or didn't -- back up. */
- X no--;
- X reginput = save + no;
- X }
- X return(0);
- X }
- X /* NOTREACHED */
- X /*break;*/
- X case END:
- X return(1); /* Success! */
- X /* NOTREACHED */
- X /*break;*/
- X default:
- X regerror("memory corruption");
- X return(0);
- X /* NOTREACHED */
- X /*break;*/
- X }
- X
- X scan = next;
- X }
- X
- X /*
- X * We get here only if there's trouble -- normally "case END" is
- X * the terminating point.
- X */
- X regerror("corrupted pointers");
- X return(0);
- X}
- X
- X/*
- X - regrepeat - repeatedly match something simple, report how many
- X */
- Xstatic int
- Xregrepeat(p)
- Xchar *p;
- X{
- X register int count = 0;
- X register char *scan;
- X register char *opnd;
- X
- X scan = reginput;
- X opnd = OPERAND(p);
- X switch (OP(p)) {
- X case ANY:
- X count = strlen(scan);
- X scan += count;
- X break;
- X case EXACTLY:
- X while (*opnd == *scan) {
- X count++;
- X scan++;
- X }
- X break;
- X case ANYOF:
- X while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
- X count++;
- X scan++;
- X }
- X break;
- X case ANYBUT:
- X while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
- X count++;
- X scan++;
- X }
- X break;
- X default: /* Oh dear. Called inappropriately. */
- X regerror("internal foulup");
- X count = 0; /* Best compromise. */
- X break;
- X }
- X reginput = scan;
- X
- X return(count);
- X}
- X
- X/*
- X - regnext - dig the "next" pointer out of a node
- X */
- Xstatic char *
- Xregnext(p)
- Xregister char *p;
- X{
- X register int offset;
- X
- X if (p == ®dummy)
- X return(NULL);
- X
- X offset = NEXT(p);
- X if (offset == 0)
- X return(NULL);
- X
- X if (OP(p) == BACK)
- X return(p-offset);
- X else
- X return(p+offset);
- X}
- X
- X#ifdef DEBUG
- X
- XSTATIC char *regprop();
- X
- X/*
- X - regdump - dump a regexp onto stdout in vaguely comprehensible form
- X */
- Xvoid
- Xregdump(r)
- Xregexp *r;
- X{
- X register char *s;
- X register char op = EXACTLY; /* Arbitrary non-END op. */
- X register char *next;
- X extern char *strchr();
- X
- X
- X s = r->program + 1;
- X while (op != END) { /* While that wasn't END last time... */
- X op = OP(s);
- X printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
- X next = regnext(s);
- X if (next == NULL) /* Next ptr. */
- X printf("(0)");
- X else
- X printf("(%d)", (s-r->program)+(next-s));
- X s += 3;
- X if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
- X /* Literal string, where present. */
- X while (*s != '\0') {
- X putchar(*s);
- X s++;
- X }
- X s++;
- X }
- X putchar('\n');
- X }
- X
- X /* Header fields of interest. */
- X if (r->regstart != '\0')
- X printf("start `%c' ", r->regstart);
- X if (r->reganch)
- X printf("anchored ");
- X if (r->regmust != NULL)
- X printf("must have \"%s\"", r->regmust);
- X printf("\n");
- X}
- X
- X/*
- X - regprop - printable representation of opcode
- X */
- Xstatic char *
- Xregprop(op)
- Xchar *op;
- X{
- X register char *p;
- X static char buf[50];
- X
- X (void) strcpy(buf, ":");
- X
- X switch (OP(op)) {
- X case BOL:
- X p = "BOL";
- X break;
- X case EOL:
- X p = "EOL";
- X break;
- X case ANY:
- X p = "ANY";
- X break;
- X case ANYOF:
- X p = "ANYOF";
- X break;
- X case ANYBUT:
- X p = "ANYBUT";
- X break;
- X case BRANCH:
- X p = "BRANCH";
- X break;
- X case EXACTLY:
- X p = "EXACTLY";
- X break;
- X case NOTHING:
- X p = "NOTHING";
- X break;
- X case BACK:
- X p = "BACK";
- X break;
- X case END:
- X p = "END";
- X break;
- X case OPEN+1:
- X case OPEN+2:
- X case OPEN+3:
- X case OPEN+4:
- X case OPEN+5:
- X case OPEN+6:
- X case OPEN+7:
- X case OPEN+8:
- X case OPEN+9:
- X sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
- X p = NULL;
- X break;
- X case CLOSE+1:
- X case CLOSE+2:
- X case CLOSE+3:
- X case CLOSE+4:
- X case CLOSE+5:
- X case CLOSE+6:
- X case CLOSE+7:
- X case CLOSE+8:
- X case CLOSE+9:
- X sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
- X p = NULL;
- X break;
- X case STAR:
- X p = "STAR";
- X break;
- X case PLUS:
- X p = "PLUS";
- X break;
- X default:
- X regerror("corrupted opcode");
- X break;
- X }
- X if (p != NULL)
- X (void) strcat(buf, p);
- X return(buf);
- X}
- X#endif
- X
- X/*
- X * The following is provided for those people who do not have strcspn() in
- X * their C libraries. They should get off their butts and do something
- X * about it; at least one public-domain implementation of those (highly
- X * useful) string routines has been published on Usenet.
- X */
- X#ifdef STRCSPN
- X/*
- X * strcspn - find length of initial segment of s1 consisting entirely
- X * of characters not from s2
- X */
- X
- Xstatic int
- Xstrcspn(s1, s2)
- Xchar *s1;
- Xchar *s2;
- X{
- X register char *scan1;
- X register char *scan2;
- X register int count;
- X
- X count = 0;
- X for (scan1 = s1; *scan1 != '\0'; scan1++) {
- X for (scan2 = s2; *scan2 != '\0';) /* ++ moved down. */
- X if (*scan1 == *scan2++)
- X return(count);
- X count++;
- X }
- X return(count);
- X}
- X#endif
- END_OF_regexp.c
- if test 27784 -ne `wc -c <regexp.c`; then
- echo shar: \"regexp.c\" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- if test -f regexp.h -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file \"regexp.h\"
- else
- echo shar: Extracting \"regexp.h\" \(574 characters\)
- sed "s/^X//" >regexp.h <<'END_OF_regexp.h'
- X/*
- X * Definitions etc. for regexp(3) routines.
- X *
- X * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
- X * not the System V one.
- X */
- X#define NSUBEXP 10
- Xtypedef struct regexp {
- X char *startp[NSUBEXP];
- X char *endp[NSUBEXP];
- X char regstart; /* Internal use only. */
- X char reganch; /* Internal use only. */
- X char *regmust; /* Internal use only. */
- X int regmlen; /* Internal use only. */
- X char program[1]; /* Unwarranted chumminess with compiler. */
- X} regexp;
- X
- Xextern regexp *regcomp();
- Xextern int regexec();
- Xextern void regsub();
- Xextern void regerror();
- END_OF_regexp.h
- if test 574 -ne `wc -c <regexp.h`; then
- echo shar: \"regexp.h\" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- if test -f regmagic.h -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file \"regmagic.h\"
- else
- echo shar: Extracting \"regmagic.h\" \(153 characters\)
- sed "s/^X//" >regmagic.h <<'END_OF_regmagic.h'
- X/*
- X * The first byte of the regexp internal "program" is actually this magic
- X * number; the start node begins in the second byte.
- X */
- X#define MAGIC 0234
- END_OF_regmagic.h
- if test 153 -ne `wc -c <regmagic.h`; then
- echo shar: \"regmagic.h\" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- if test -f trmoves.c -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file \"trmoves.c\"
- else
- echo shar: Extracting \"trmoves.c\" \(20210 characters\)
- sed "s/^X//" >trmoves.c <<'END_OF_trmoves.c'
- X/*
- X * Copyright (c) 1986 by Ray Balogh (rabalogh@ccng.waterloo.edu)
- X *
- X * Permission is granted to anyone to use this software for any
- X * purpose on any computer system, and to redistribute it freely,
- X * subject to the following restrictions:
- X *
- X * 1. The author is not responsible for the consequences of use of
- X * this software, no matter how awful, even if they arise
- X * from defects in it.
- X *
- X * 2. The origin of this software must not be misrepresented, either
- X * by explicit claim or by omission.
- X *
- X * 3. Altered versions must be plainly marked as such, and must not
- X * be misrepresented as being the original software.
- X *
- X */
- X
- X/* The following copyright notice appears in the chess program from which
- X * legal move generation code was derived:
- X *
- X C source for CHESS Rev. 3-10-87
- X
- X Written by John Stanback (hplabs!hpfcla!hpisla!hpltca!jhs)
- X
- X Patches for BSD Unix by Rich Salz (rs@mirror.TMC.COM) - 5/3/87
- X *
- X *
- X ****************************************************************************
- X */
- X
- X
- X#include <stdio.h>
- X
- X#define true 1
- X#define false 0
- X#define neutral 0
- X#define white 1
- X#define black 2
- X#define no_piece 0
- X#define pawn 1
- X#define knight 2
- X#define bishop 3
- X#define rook 4
- X#define queen 5
- X#define king 6
- X#define px " PNBRQK"
- X#define qx " pnbrqk"
- X#define rx "12345678"
- X#define cx "abcdefgh"
- X#define check 0x00000001
- X#define capture 0x00000002
- X#define draw 0x00000004
- X#define promote_n 0x00000010
- X#define promote_b 0x00000020
- X#define promote_r 0x00000040
- X#define promote_q 0x00000080
- X#define promote (promote_n|promote_b|promote_r|promote_q)
- X#define incheck 0x00000100
- X#define epmask 0x00000200
- X#define exact 0x00000400
- X#define pwnthrt 0x00000800
- X
- X#define MAXMVSTR 12
- X#define DUNNO '_'
- X#define ETC '*'
- X#define BADCHAR '\004'
- X
- X#define PLYSIDE(ply) (ply % 2 ? "black" : "white")
- X
- X
- Xstruct leaf
- X {
- X int f,t;
- X unsigned int flags;
- X };
- X
- Xint row[64],col[64],Index[64];
- Xint PieceList[3][16],PieceCnt[3];
- Xint castld[3],kingmoved[3],mtl[3],pmtl[3];
- Xint qrookmoved[3],krookmoved[3];
- Xint PawnCnt[3][8];
- Xint GameCnt,epsquare = -1;
- Xunsigned int GameList[240];
- Xint GamePc[240],GameClr[240];
- Xint GamePiece[240],GameColor[240],GamePromote[240];
- Xint value[8]={0,100,330,330,500,950,999};
- Xint otherside[3]={0,2,1};
- Xint map[64]=
- X {26,27,28,29,30,31,32,33,38,39,40,41,42,43,44,45,
- X 50,51,52,53,54,55,56,57,62,63,64,65,66,67,68,69,
- X 74,75,76,77,78,79,80,81,86,87,88,89,90,91,92,93,
- X 98,99,100,101,102,103,104,105,110,111,112,113,114,115,116,117};
- Xint unmap[144]=
- X {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- X -1,-1,0,1,2,3,4,5,6,7,-1,-1,-1,-1,8,9,10,11,12,13,14,15,-1,-1,
- X -1,-1,16,17,18,19,20,21,22,23,-1,-1,-1,-1,24,25,26,27,28,29,30,31,-1,-1,
- X -1,-1,32,33,34,35,36,37,38,39,-1,-1,-1,-1,40,41,42,43,44,45,46,47,-1,-1,
- X -1,-1,48,49,50,51,52,53,54,55,-1,-1,-1,-1,56,57,58,59,60,61,62,63,-1,-1,
- X -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
- Xint board[64]=
- X {rook,knight,bishop,queen,king,bishop,knight,rook,
- X pawn,pawn,pawn,pawn,pawn,pawn,pawn,pawn,
- X 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- X pawn,pawn,pawn,pawn,pawn,pawn,pawn,pawn,
- X rook,knight,bishop,queen,king,bishop,knight,rook};
- Xint color[64]=
- X {white,white,white,white,white,white,white,white,
- X white,white,white,white,white,white,white,white,
- X 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- X black,black,black,black,black,black,black,black,
- X black,black,black,black,black,black,black,black};
- Xint sweep[7]= {false,false,false,true,true,true,false};
- Xint Dstpwn[3]={0,4,6};
- Xint Dstart[7]={6,4,8,4,0,0,0};
- Xint Dstop[7]={7,5,15,7,3,7,7};
- Xint Dir[16]={1,12,-1,-12,11,13,-11,-13,10,-10,14,-14,23,-23,25,-25};
- Xint MovePnt = 0;
- Xstruct leaf Moves[2000];
- Xchar charmap[] = " PNBRQK";
- Xint real_ep_square = -1;
- X
- XInitializeStats()
- X{
- Xregister int i,loc;
- Xint l,r,c;
- X
- X for (r = 0; r < 8; r++)
- X for (c = 0; c < 8; c++)
- X {
- X l = 8*r+c;
- X row[l] = r; col[l] = c;
- X }
- X for (i = 0; i < 8; i++)
- X PawnCnt[white][i] = PawnCnt[black][i] = 0;
- X
- X /*epsquare = -1;*/
- X GameCnt = -1;
- X castld[white] = castld[black] = false;
- X kingmoved[white] = kingmoved[black] = 0;
- X krookmoved[white] = krookmoved[black] = 0;
- X qrookmoved[white] = qrookmoved[black] = 0;
- X
- X mtl[white] = mtl[black] = pmtl[white] = pmtl[black]=0;
- X PieceCnt[white] = PieceCnt[black] = 0;
- X for (loc = 0; loc < 64; loc++)
- X if (color[loc] != neutral)
- X {
- X mtl[color[loc]] += value[board[loc]];
- X if (board[loc] == pawn)
- X {
- X pmtl[color[loc]] += value[pawn];
- X ++PawnCnt[color[loc]][col[loc]];
- X }
- X if (board[loc] == king) Index[loc] = 0;
- X else Index[loc] = ++PieceCnt[color[loc]];
- X PieceList[color[loc]][Index[loc]] = loc;
- X }
- X}
- X
- X
- XMakeMove(side,node,tempb,tempc)
- Xint side,*tempc,*tempb;
- Xstruct leaf *node;
- X
- X/*
- X Update Arrays board[], color[], and Index[] to reflect the new
- X board position obtained after making the move pointed to by
- X node. Also update miscellaneous stuff that changes when a move
- X is made.
- X*/
- X
- X{
- Xregister int f,t;
- Xint ok,xside;
- Xint prom_val;
- Xint bf, bt;
- X
- X xside = otherside[side];
- X epsquare = -1;
- X f = node->f; t = node->t;
- X GameList[++GameCnt] = (f<<8) + t;
- X if (f == 255 || t == 255)
- X {
- X GamePc[GameCnt] = no_piece; GameClr[GameCnt] = neutral;
- X GamePiece[GameCnt] = no_piece; GameColor[GameCnt] = neutral;
- X GamePromote[GameCnt] = no_piece;
- X castle(side,f,t,1,&ok);
- X }
- X else
- X {
- X bf = board[f]; bt = board[t];
- X *tempc = color[t]; *tempb = bt;
- X GamePc[GameCnt] = *tempb; GameClr[GameCnt] = *tempc;
- X GamePiece[GameCnt] = bf; GameColor[GameCnt] = color[f];
- X GamePromote[GameCnt] = prompiece( node->flags );
- X if (*tempc != neutral)
- X {
- X UpdatePieceList(*tempc,t,1);
- X if (*tempb == pawn) --PawnCnt[*tempc][col[t]];
- X if (bf == pawn)
- X {
- X --PawnCnt[side][col[f]];
- X ++PawnCnt[side][col[t]];
- X }
- X mtl[xside] -= value[*tempb];
- X if (*tempb == pawn) pmtl[xside] -= value[pawn];
- X }
- X if (bf == king) ++kingmoved[side];
- X if (f==0 && bf==rook || t==0 && bt==rook) ++qrookmoved[white];
- X if (f==7 && bf==rook || t==7 && bt==rook) ++krookmoved[white];
- X if (f==56 && bf==rook || t==56 && bt==rook) ++qrookmoved[black];
- X if (f==63 && bf==rook || t==63 && bt==rook) ++krookmoved[black];
- X
- X color[t] = color[f]; board[t] = bf;
- X Index[t] = Index[f]; PieceList[side][Index[t]] = t;
- X color[f] = neutral; board[f] = no_piece;
- X if (board[t] == pawn)
- X if (t-f == 16) epsquare = f+8;
- X else if (f-t == 16) epsquare = f-8;
- X if (node->flags & promote)
- X {
- X prom_val = prompiece( node->flags );
- X board[t] = prom_val;
- X mtl[side] += value[prom_val] - value[pawn];
- X pmtl[side] -= value[pawn];
- X }
- X if (node->flags & epmask) en_passant(side,xside,f,t,1);
- X }
- X}
- X
- X
- XUnmakeMove(side,node,tempb,tempc)
- Xint side,*tempc,*tempb;
- Xstruct leaf *node;
- X
- X/*
- X Take back the move pointed to by node.
- X*/
- X
- X{
- Xregister int f,t;
- Xint ok,xside;
- Xint prom_val;
- Xint bf, bt;
- X
- X xside = otherside[side];
- X epsquare = -1;
- X f = node->f; t = node->t;
- X GameCnt--;
- X if (f == 255 || t == 255) castle(side,f,t,2,&ok);
- X else
- X {
- X if (board[t] == king) --kingmoved[side];
- X bf = board[f]; bt = board[t];
- X if (f==0 && bt==rook || t==0 && bf==rook) --qrookmoved[white];
- X if (f==7 && bt==rook || t==7 && bf==rook) --krookmoved[white];
- X if (f==56 && bt==rook || t==56 && bf==rook) --qrookmoved[black];
- X if (f==63 && bt==rook || t==63 && bf==rook) --krookmoved[black];
- X
- X color[f] = color[t]; board[f] = board[t];
- X Index[f] = Index[t]; PieceList[side][Index[f]] = f;
- X color[t] = *tempc; board[t] = *tempb;
- X if (*tempc != neutral)
- X {
- X UpdatePieceList(*tempc,t,2);
- X if (*tempb == pawn) ++PawnCnt[*tempc][col[t]];
- X if (board[f] == pawn)
- X {
- X --PawnCnt[side][col[t]];
- X ++PawnCnt[side][col[f]];
- X }
- X mtl[xside] += value[*tempb];
- X if (*tempb == pawn) pmtl[xside] += value[pawn];
- X }
- X if (node->flags & promote)
- X {
- X prom_val = prompiece( node->flags );
- X board[f] = pawn;
- X mtl[side] += value[pawn] - value[prom_val];
- X pmtl[side] += value[pawn];
- X }
- X if (node->flags & epmask) en_passant(side,xside,f,t,2);
- X }
- X}
- X
- X
- Xint
- Xprompiece( promflags )
- Xint promflags;
- X{
- X return( (promflags & promote_q) ? queen :
- X (promflags & promote_r) ? rook :
- X (promflags & promote_b) ? bishop :
- X (promflags & promote_n) ? knight :
- X no_piece
- X );
- X}
- X
- X
- XUpdatePieceList(side,loc,iop)
- Xint side,loc,iop;
- X
- X/*
- X Array PieceList[side][indx] contains the location of all the pieces of
- X either side. Array Index[loc] contains the indx into PieceList for a
- X given square.
- X*/
- X
- X{
- Xregister int i;
- X if (iop == 1)
- X {
- X PieceCnt[side]--;
- X for (i = Index[loc]; i <= PieceCnt[side]; i++)
- X {
- X PieceList[side][i] = PieceList[side][i+1];
- X Index[PieceList[side][i]] = i;
- X }
- X }
- X else
- X {
- X PieceCnt[side]++;
- X PieceList[side][PieceCnt[side]] = loc;
- X Index[loc] = PieceCnt[side];
- X }
- X}
- X
- X
- Xcastle(side,f,t,iop,ok)
- Xint side,f,t,iop,*ok;
- X{
- Xint i,e,k1,k2,r1,r2,c1,c2,t0,xside;
- X
- X xside = otherside[side];
- X if (side == white) e = 0; else e = 56;
- X if (f == 255)
- X {
- X /* castle king's side */
- X k1 = e+4; k2 = e+6; r1 = e+7; r2 = e+5; c1 = k1; c2 = r1;
- X }
- X else
- X {
- X /* castle queen's side */
- X k1 = e+4; k2 = e+2; r1 = e; r2 = e+3; c1 = r1; c2 = k1;
- X }
- X if (iop == 0)
- X {
- X *ok = false;
- X if (f == 255)
- X {
- X if (castld[side] || krookmoved[side] > 0 || kingmoved[side] > 0)
- X return;
- X }
- X else
- X {
- X if (castld[side] || qrookmoved[side] > 0 || kingmoved[side] > 0)
- X return;
- X }
- X if (board[k1] == king && board[r1] == rook)
- X {
- X *ok = true;
- X for (i = c1; i <= c2; i++)
- X if (isincheck(side,i)) {*ok = false; break;}
- X if (*ok)
- X for (i = c1+1; i < c2; i++)
- X if (color[i] != neutral) {*ok = false; break;}
- X }
- X }
- X else
- X {
- X if (iop == 1) castld[side] = true; else castld[side] = false;
- X if (iop == 2)
- X {
- X t0 = k1; k1 = k2; k2 = t0;
- X t0 = r1; r1 = r2; r2 = t0;
- X }
- X board[k2] = king; color[k2] = side; Index[k2] = 0;
- X board[k1] = no_piece; color[k1] = neutral;
- X board[r2] = rook; color[r2] = side; Index[r2] = Index[r1];
- X board[r1] = no_piece; color[r1] = neutral;
- X PieceList[side][Index[k2]] = k2;
- X PieceList[side][Index[r2]] = r2;
- X }
- X}
- X
- X
- Xen_passant(side,xside,f,t,iop)
- Xint side,f,t,iop;
- X{
- Xint l;
- X if (t > f) l = t-8; else l = t+8;
- X if (iop == 1)
- X {
- X board[l] = no_piece; color[l] = neutral;
- X }
- X else
- X {
- X board[l] = pawn; color[l] = xside;
- X }
- X InitializeStats();
- X}
- X
- Xint
- Xisincheck(side,kingloc)
- Xint side,kingloc;
- X
- X/*
- X Determine if king is in check (efficiently).
- X*/
- X
- X{
- X register int u, d, m, j, loc;
- X int co, ro;
- X int xside;
- X int stop;
- X int m0;
- X int piece;
- X
- X xside = otherside[side];
- X /*kingloc = PieceList[side][0];*/
- X co = col[kingloc]; ro = row[kingloc];
- X /* check for pawn checks */
- X if ( side == white ) {
- X if ( ro < 6 ) {
- X if ( co > 0 && color[kingloc+7] == xside && board[kingloc+7] == pawn )
- X return( true );
- X if ( co < 7 && color[kingloc+9] == xside && board[kingloc+9] == pawn )
- X return( true );
- X }
- X } else {
- X if ( ro > 1 ) {
- X if ( co < 7 && color[kingloc-7] == xside && board[kingloc-7] == pawn )
- X return( true );
- X if ( co > 0 && color[kingloc-9] == xside && board[kingloc-9] == pawn )
- X return( true );
- X }
- X }
- X /* check for knight checks */
- X m0 = map[kingloc]; stop = Dstop[knight];
- X for ( j = Dstart[knight]; j <= stop; j++ ) {
- X if ( (loc = unmap[m0+Dir[j]]) < 0 ) continue;
- X if ( board[loc] == knight && color[loc] == xside )
- X return( true );
- X }
- X /* check for bishop, rook and queen checks */
- X for ( piece = bishop; piece <= rook; piece+=(rook-bishop) )
- X {
- X stop = Dstop[piece];
- X for (j = Dstart[piece]; j <= stop; j++)
- X {
- X d = Dir[j]; m = m0+d; u = unmap[m];
- X while (u >= 0)
- X {
- X if (color[u] == neutral)
- X {
- X m += d; u = unmap[m];
- X }
- X else
- X {
- X if ( (board[u] == piece || board[u] == queen)
- X && color[u] == xside ) return( true );
- X u = -1;
- X }
- X }
- X }
- X }
- X return( false );
- X}
- X
- X
- XLinkMove(f,t,side,promoteto)
- Xint f,t,side,promoteto;
- X{
- X
- X/*
- X Add a move to the tree.
- X*/
- X
- Xstruct leaf *node;
- X
- Xstruct leaf tnode;
- Xint tempb, tempc;
- X
- X if ( !(f == 255 || t == 255) ) {
- X /* Don't bother to link the move if the king is in check afterwards;
- X ie. it is really illegal */
- X tnode.f = f; tnode.t = t;
- X tnode.flags = 0;
- X MakeMove(side,&tnode,&tempb,&tempc);
- X if (isincheck(side,PieceList[side][0]))
- X {
- X UnmakeMove(side,&tnode,&tempb,&tempc);
- X return;
- X }
- X else
- X UnmakeMove(side,&tnode,&tempb,&tempc);
- X }
- X
- X node = &Moves[MovePnt];
- X ++MovePnt;
- X node->flags = 0;
- X node->f = f; node->t = t;
- X
- X if ( !(f == 255 || t == 255) )
- X {
- X if (color[t] != neutral)
- X {
- X node->flags |= capture;
- X }
- X if (board[f] == pawn)
- X {
- X if (row[t] == 0 || row[t] == 7) node->flags |= promoteto;
- X else if (t == real_ep_square) node->flags |= epmask;
- X }
- X }
- X}
- X
- X
- XGenMoves(loc,side,xside)
- Xint loc,side,xside;
- X
- X/*
- X Generate moves for a piece. The from square is mapped onto a 12 by
- X 12 board and offsets (taken from array Dir[]) are added to the
- X mapped location. Array unmap[] maps the move back onto array
- X board[] (yielding a value of -1 if the to square is off the board).
- X This process is repeated for bishops, rooks, and queens until a
- X piece is encountered or the the move falls off the board. Legal
- X moves are then linked into the tree.
- X*/
- X
- X{
- Xregister int m,u,d;
- Xint i,m0,piece;
- X
- X piece = board[loc]; m0 = map[loc];
- X
- X if (sweep[piece])
- X {
- X for (i = Dstart[piece]; i <= Dstop[piece]; i++)
- X {
- X d = Dir[i]; m = m0+d; u = unmap[m];
- X while (u >= 0)
- X if (color[u] == neutral)
- X {
- X LinkMove(loc,u,side,no_piece);
- X m += d; u = unmap[m];
- X }
- X else if (color[u] == xside)
- X {
- X LinkMove(loc,u,side,no_piece);
- X u = -1;
- X }
- X else u = -1;
- X }
- X }
- X else if (piece == pawn)
- X {
- X if (side == white && color[loc+8] == neutral)
- X {
- X if (row[loc] == 6)
- X {
- X LinkMove(loc,loc+8,side,promote_q);
- X LinkMove(loc,loc+8,side,promote_r);
- X LinkMove(loc,loc+8,side,promote_b);
- X LinkMove(loc,loc+8,side,promote_n);
- X }
- X else
- X LinkMove(loc,loc+8,side,no_piece);
- X if (row[loc] == 1)
- X if (color[loc+16] == neutral)
- X LinkMove(loc,loc+16,side,no_piece);
- X }
- X else if (side == black && color[loc-8] == neutral)
- X {
- X if (row[loc] == 1)
- X {
- X LinkMove(loc,loc-8,side,promote_q);
- X LinkMove(loc,loc-8,side,promote_r);
- X LinkMove(loc,loc-8,side,promote_b);
- X LinkMove(loc,loc-8,side,promote_n);
- X }
- X else
- X LinkMove(loc,loc-8,side,no_piece);
- X if (row[loc] == 6)
- X if (color[loc-16] == neutral)
- X LinkMove(loc,loc-16,side,no_piece);
- X }
- X for (i = Dstart[piece]; i <= Dstop[piece]; i++)
- X if ((u = unmap[m0+Dir[i]]) >= 0)
- X if (color[u] == xside || u == real_ep_square)
- X if (side==white && row[loc]==6 || side==black && row[loc]==1)
- X {
- X LinkMove(loc,u,side,promote_q);
- X LinkMove(loc,u,side,promote_r);
- X LinkMove(loc,u,side,promote_b);
- X LinkMove(loc,u,side,promote_n);
- X }
- X else
- X LinkMove(loc,u,side,no_piece);
- X }
- X else
- X {
- X for (i = Dstart[piece]; i <= Dstop[piece]; i++)
- X if ((u = unmap[m0+Dir[i]]) >= 0)
- X if (color[u] != side)
- X LinkMove(loc,u,side,no_piece);
- X }
- X}
- X
- X
- X
- XMoveList(side)
- Xint side;
- X
- X/*
- X Fill the array Moves[] with all available moves for side to
- X play. Array MovePnt contains the index into Moves[].
- X*/
- X
- X{
- Xregister int i;
- Xint ok,xside;
- Xstatic int initted = false;
- X
- X if ( !initted ) {
- X InitializeStats();
- X initted = true;
- X }
- X
- X MovePnt = 0;
- X xside = otherside[side];
- X Dstart[pawn] = Dstpwn[side]; Dstop[pawn] = Dstart[pawn] + 1;
- X
- X for (i = 0; i <= PieceCnt[side]; i++)
- X GenMoves(PieceList[side][i],side,xside);
- X
- X castle(side,255,0,0,&ok);
- X if (ok) LinkMove(255,0,side,no_piece);
- X castle(side,0,255,0,&ok);
- X if (ok) LinkMove(0,255,side,no_piece);
- X
- X}
- X
- X
- XToAlgNot( form1, form2, form3, node, side )
- Xchar form1[], form2[], form3[];
- Xstruct leaf *node;
- X{
- X int f, t, fr, tr;
- X char ff, tf, piece, cap;
- X char prom;
- X
- X f = node->f; t = node->t;
- X if ( f == 255 ) { /* castle king's side */
- X ff = 'e'; tf = 'g';
- X fr = tr = (side == white) ? 1 : 8;
- X cap = '-';
- X piece = 'K';
- X } else if ( t == 255 ) { /* castle queen's side */
- X ff = 'e'; tf = 'c';
- X fr = tr = (side == white) ? 1 : 8;
- X cap = '-';
- X piece = 'K';
- X } else {
- X ff = col[f] + 'a'; tf = col[t] + 'a';
- X fr = row[f] + 1; tr = row[t] + 1;
- X cap = node->flags&(capture|epmask) ? ':' : '-';
- X piece = charmap[board[f]];
- X }
- X switch ( node->flags & promote ) {
- X case promote_n: prom = 'N'; break;
- X case promote_b: prom = 'B'; break;
- X case promote_r: prom = 'R'; break;
- X case promote_q:
- X prom = 'Q';
- X sprintf( form2, "%c%c%d%c%c%d*", piece, ff, fr, cap, tf, tr );
- X break;
- X default: prom = '*';
- X }
- X sprintf( form1, "%c%c%d%c%c%d%c*", piece, ff, fr, cap, tf, tr, prom );
- X sprintf( form3, "%c%d%c%d", ff, fr, tf, tr );
- X if ( prom != 'Q' )
- X strcpy( form2, form1 );
- X}
- X
- X
- Xint
- XMvComp( incomp, reference )
- Xchar *incomp, *reference;
- X{
- X for ( ; *incomp == *reference || *incomp == DUNNO; incomp++, reference++ )
- X if ( *incomp == '\0' || *reference == '\0' )
- X return( true );
- X if ( *incomp == ETC ||
- X (*reference == ETC &&
- X (*incomp == '\0' || index("nbrqNBRQ",*incomp) == NULL)) )
- X return( true );
- X return( false );
- X}
- X
- X
- Xint
- XDoMove(side, move, ply)
- Xint side;
- Xchar move[];
- Xint ply;
- X{
- X int i, j, n;
- X int tempb, tempc;
- X char expandp[MAXMVSTR], expand[MAXMVSTR], compact[MAXMVSTR];
- X char gexpand[MAXMVSTR], gcompact[MAXMVSTR];
- X struct leaf *gnode;
- X
- X
- X MoveList( side );
- X
- X for ( n = 0, i = 0; i < MovePnt; i++ ) {
- X ToAlgNot( expandp, expand, compact, &Moves[i], side );
- X if ( MvComp(move,expandp) || MvComp(move,expand) ) {
- X#ifdef DEBUG
- X printf( "%s,%s,%s,%s\n", move, expandp, expand, compact );
- X#endif
- X strcpy( gexpand, expand );
- X strcpy( gcompact, compact );
- X gnode = &Moves[i];
- X n++;
- X }
- X }
- X
- X switch ( n ) {
- X case 0:
- X#ifdef VERBOSE
- X printf( "move %d, %s: illegal move (%s) -- legal moves are:\n",
- X ply/2+1, PLYSIDE(ply), move );
- X for ( i = 0; i < MovePnt; ) {
- X printf(" ");
- X for (j = 0; j < 8 && i < MovePnt; j++, i++) {
- X ToAlgNot( expandp, expand, compact, &Moves[i], side );
- X printf( " %s", expand );
- X }
- X printf("\n");
- X }
- X#else
- X printf( "illegal move\n" );
- X#endif
- X return( false );
- X case 1:
- X printf( "%s\n", gcompact ); fflush( stdout );
- X MakeMove( side, gnode, &tempb, &tempc );
- X real_ep_square = epsquare;
- X return( true );
- X default:
- X#ifdef VERBOSE
- X printf( "move %d, %s: ambiguous move (%s) matches:\n",
- X ply/2+1, PLYSIDE(ply), move );
- X for (i = 0; i < MovePnt;) {
- X printf(" ");
- X for (j = 0; j < 8 && i < MovePnt; j++, i++) {
- X ToAlgNot( expandp, expand, compact, &Moves[i], side );
- X if ( MvComp(move,expandp) || MvComp(move,expand) )
- X printf( " %s\n", expand );
- X }
- X printf( "\n" );
- X }
- X#else
- X printf( "ambiguous move\n" );
- X#endif
- X return( false );
- X }
- X}
- X
- X
- Xtrans(move, ply)
- Xchar move[];
- Xint ply;
- X{
- X static int side = white;
- X
- X if ( !DoMove(side,move,ply) )
- X return;
- X side = otherside[side];
- X}
- END_OF_trmoves.c
- if test 20210 -ne `wc -c <trmoves.c`; then
- echo shar: \"trmoves.c\" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- echo shar: End of shell archive.
- exit 0
-